//
//  YZComplaintsController.m
//  优品美妆
//
//  Created by apple on 16/4/7.
//  Copyright © 2016年 RongChunagYin. All rights reserved.
//

#import "LSComplaintsController.h"
#import "LSReportVCTableViewController.h"

#define Main_Color [UIColor colorWithRed:247.0/255.0 green:247.0/255.0 blue:247.0/255.0 alpha:1]
#define MAIN_COLOR  [UIColor colorWithRed:0.0/255.0f green:180.0/255.0f blue:222.0/255.0f alpha:1.0f]
@interface LSComplaintsController ()<UITextViewDelegate>
@property (strong ,nonatomic)UILabel *tip;
@property (strong, nonatomic)UILabel *count;
@property (strong, nonatomic)UITextView *textView;
@end

@implementation LSComplaintsController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = [NSString stringWithFormat:@"举报%@",self.topStr];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    
    self.view.backgroundColor = Main_Color;
    self.tableView.backgroundColor = Main_Color;
    
    //设置返回按钮
    UIBarButtonItem *leftItem = [UIBarButtonItem itemWithTarget:self action:@selector(leftBarButtonItemClick) image:@"Back" highImage:@"Back"];
    self.navigationItem.leftBarButtonItem = leftItem;
    
 }

-(void)leftBarButtonItemClick{
    
    [self.navigationController popViewControllerAnimated:YES];
    
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return 1;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *cellStr = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellStr forIndexPath:indexPath];
    
    if (indexPath.section == 0 && indexPath.row == 0) {
        
        cell.textLabel.text = self.topStr;
        cell.selectionStyle = 0;
        cell.textLabel.font =[UIFont systemFontOfSize:14];
        cell.tintColor = [UIColor colorWithRed:255/255.0 green:100/255.0 blue:190/255.0 alpha:1.0];
        
    }else if (indexPath.section == 1 && indexPath.row ==0){
        
        /*创建投诉文本框*/
        UITextView *textView = [[UITextView alloc]initWithFrame:cell.bounds];
        textView.text = @"";
        textView.font = [UIFont fontWithName:@"Arial" size:12.0f];
        textView.backgroundColor = [UIColor whiteColor];
        textView.textAlignment = NSTextAlignmentLeft;
        textView.autocapitalizationType = UITextAutocapitalizationTypeNone;
        textView.returnKeyType = UIReturnKeyDefault;
        textView.scrollEnabled = YES;
        textView.delegate = self;
        self.textView = textView;
        /*自定义文本框placeholder*/
        _tip = [[UILabel alloc]initWithFrame:CGRectMake(10, 3, self.view.width, 25)];
        _tip.text = @"输入你对他的投诉描述(仅限50字)";
        _tip.font = [UIFont fontWithName:@"Arial" size:12.0f];
        _tip.backgroundColor = [UIColor clearColor];
        _tip.enabled = NO;
        /*自定义文本框字数统计*/
        _count = [[UILabel alloc]initWithFrame:CGRectMake(textView.width - 40, textView.height - 30, 40, 30)];
        _count.text = @"50";
        _count.textAlignment = NSTextAlignmentRight;
        _count.font = [UIFont fontWithName:@"Arial" size:12.0f];
        _count.backgroundColor = [UIColor clearColor];
        _count.enabled = NO;
        
        [cell addSubview:_textView];
        [cell addSubview:_tip];
        [cell addSubview:_count];
    }
    
    return cell;
}


-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.width, 30)];
    view.backgroundColor = Main_Color;
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, view.width, 20)];
    label.textColor = [UIColor grayColor];
    label.backgroundColor = Main_Color;
    
    label.font = [UIFont systemFontOfSize:12];
    [view addSubview:label];
    switch (section) {
        case 0:
            label.text = @"投诉内容";
            return view;
            break;
            
            case 1:
            label.text = @"投诉描述";
            return view;
            break;
        default:
            break;
    }
    
    
    return nil;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    if (section == 1) {
        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.width, 36 + 20)];
        view.backgroundColor = Main_Color;
        
        UIButton *sendBtn = [[UIButton alloc]initWithFrame:CGRectMake(10, 10, self.view.width - 20, 36)];
        [sendBtn setBackgroundColor:MAIN_COLOR];
        [sendBtn setTitle:@"发送" forState:UIControlStateNormal];
        [sendBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [sendBtn addTarget:self action:@selector(sendBtn:) forControlEvents:UIControlEventTouchUpInside];
        sendBtn.layer.cornerRadius = 10.f;
        
        [view addSubview:sendBtn];
        
        return view;

    }else return nil;
    
}

#pragma mark - 发送按钮
-(void)sendBtn:(UIButton *)btn{
    
    if (self.textView.text.length > 0) {
        
        [self showRoundProgressWithTitle:@"请稍后..."];
        
        NSMutableDictionary * dic = [NSMutableDictionary  dictionary];
        
        NSString * reason = [NSString stringWithFormat:@"%@%@",self.topStr,self.textView];
        
        [dic setObject:[USER_DEFAULTS objectForKey:@"userid"] forKey:@"userId"];
        [dic setObject:[USER_DEFAULTS objectForKey:@"token"] forKey:@"token"];//验证用户
        [dic setObject:self.userName forKey:@"username"];
        [dic setObject:self.resId forKey:@"resId"];
        [dic setObject:reason forKey:@"reason"];

        [LSHttpTool sendHttpData:@"complaintsBid_app_cibd.im.lensum" params:dic success:^(id responseObject) {
            
            NSDictionary *dicInfo = [NSDictionary dictionaryWithDictionary:responseObject];
            
            if ([[dicInfo objectForKey:@"result"] integerValue] == 1) {
                
                self.textView.text = @"";
                [self.navigationController popToRootViewControllerAnimated:YES];

                [self showRightWithTitle:@"提交成功" autoCloseTime:2];
                
            }else{

                [self showSingleIconBubble:[dicInfo objectForKey:@"msg"]];
                
            }
            
           
            
        } failure:^(NSError *error) {

            [self showSingleIconBubble:@"网络连接错误"];
            
        }];

    }else{

       [self showSingleIconBubble:@"网络连接错误"];
    
    }
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return 60;
    }else if (indexPath.section == 1){
        return 100;
    }else return 0;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (section == 1) {
        
    return 36;
        
    }else{
        
        return 1;
        
    }
}

- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 30;
}
#pragma mark - textView 

// 自定义文本框placeholder

- (void)textViewDidChange:(UITextView *)textView {
    NSInteger number = [_textView.text length];
    if (number >= 50) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"字符个数不能大于50" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [alert show];
        _textView.text = [_textView.text substringToIndex:50];
        number = 50;
     
    }
    self.count.text = [NSString stringWithFormat:@"%ld/50",(long)number];
    
    if (self.textView.text.length > 0) {
        _tip.text = @"";
        
    }else{
        _tip.text = @"输入你对他的投诉描述(仅限50字)";
    }
}

@end
